﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("OpenPhotoAlbum.Controls");

OpenPhotoAlbum.Controls.InplaceEditLabel = function(element) {
	OpenPhotoAlbum.Controls.InplaceEditLabel.initializeBase(this, [element]);

	this._Text = null;
	this._IsEditing = null;

	this._labelElement = null;
	this._textBoxElement = null;
	this._saveElement = null;
	this._cancelElement = null;

	this._Save = null;
	this._Cancel = null;
	this._ContextData1 = null;
	this._ContextData2 = null;
	this._ContextData3 = null;
	this._IsMultiLine = null;
	this._TooltipBehaviorId = null;
}

OpenPhotoAlbum.Controls.InplaceEditLabel.prototype = {
	initialize: function() {
		OpenPhotoAlbum.Controls.InplaceEditLabel.callBaseMethod(this, 'initialize');

		var labelId = this._element.id + "_label";
		var textBoxId = this._element.id + "_textBox";
		var saveButtonId = this._element.id + "_save";
		var cancelButtonId = this._element.id + "_cancel";

		var html = '';
		html += String.format(
			'<span id="{0}" class="label" style="display:none;"></span>',
			labelId);
		if (!this._IsMultiLine) {
			html += String.format(
				'<input id="{0}" type="text" class="textBox" style="display:none;" />',
				textBoxId);
		}
		else {
			html += String.format(
				'<textarea id="{0}" type="text" class="textBox" rows="3" cols="30" style="display:none;"></textarea>',
				textBoxId);
		}
		html += String.format(
			'<input id="{0}" type="button" value="{1}" class="save" style="display:none;" />',
			saveButtonId,
			this._Save);
		html += String.format(
			'<input id="{0}" type="button" value="{1}" class="cancel" style="display:none;" />',
			cancelButtonId,
			this._Cancel);
		this._element.innerHTML = html;

		this._labelElement = $get(labelId);
		this._textBoxElement = $get(textBoxId);
		this._saveElement = $get(saveButtonId);
		this._cancelElement = $get(cancelButtonId);

		setText(this._labelElement, this._Text);
		setText(this._textBoxElement, this._Text);

		$addHandler(this._labelElement, "click", Function.createDelegate(this, this._onLabelClicked));
		$addHandler(this._textBoxElement, "keydown", Function.createDelegate(this, this._onTextBoxKeyDown));
		$addHandler(this._saveElement, "click", Function.createDelegate(this, this._saveChanges));
		$addHandler(this._cancelElement, "click", Function.createDelegate(this, this._cancelChanges));

		if (this._IsEditing) { this._showTextBox(); }
		else { this._showLabel(); }
	},
	dispose: function() {
		$clearHandlers(this._labelElement);
		$clearHandlers(this._textBoxElement);
		$clearHandlers(this._saveElement);
		$clearHandlers(this._cancelElement);

		OpenPhotoAlbum.Controls.InplaceEditLabel.callBaseMethod(this, 'dispose');
	},

	_showTextBox: function() {
		this._labelElement.style.display = 'none';
		this._textBoxElement.style.display = '';
		this._textBoxElement.focus();
		this._textBoxElement.select();
		this._saveElement.style.display = '';
		this._cancelElement.style.display = '';

		if (this._TooltipBehaviorId) {
			var tooltip = $find(this._TooltipBehaviorId);
			if (tooltip) { tooltip.disableTooltip(); }
		}
	},

	_showLabel: function() {
		this._labelElement.style.display = '';
		this._textBoxElement.style.display = 'none';
		this._saveElement.style.display = 'none';
		this._cancelElement.style.display = 'none';

		if (this._TooltipBehaviorId) {
			var tooltip = $find(this._TooltipBehaviorId);
			if (tooltip) { tooltip.enableTooltip(); }
		}
	},

	_onLabelClicked: function() {
		this._showTextBox();
	},

	_saveChanges: function() {
		var newText = getText(this._textBoxElement);
		var oldText = getText(this._labelElement);
		if (newText != oldText) {
			setText(this._labelElement, newText);
			var eventArgs = new Sys.EventArgs();
			eventArgs.oldText = oldText;
			eventArgs.newText = newText;
			eventArgs.contextData1 = this._ContextData1;
			eventArgs.contextData2 = this._ContextData2;
			eventArgs.contextData3 = this._ContextData3;
			this._raiseEvent('OnClientTextChanged', eventArgs);
		}
		this._showLabel();
	},

	_cancelChanges: function() {
		var previousText = getText(this._labelElement);
		setText(this._textBoxElement, previousText);
		this._showLabel();
	},

	_onTextBoxKeyDown: function(event) {
		if (event.keyCode == 13) {
			if (!this._IsMultiLine) {
				event.preventDefault();
				this._saveChanges();
			}
		}
		else if (event.keyCode == 27) {
			event.preventDefault();
			this._cancelChanges();
		}
	},

	get_Text: function() { return this._Text; },
	set_Text: function(value) { this._Text = value; },

	get_IsEditing: function() { return this._IsEditing; },
	set_IsEditing: function(value) { this._IsEditing = value; },

	add_OnClientTextChanged: function(handler) { this.get_events().addHandler('OnClientTextChanged', handler); },
	remove_OnClientTextChanged: function(handler) { this.get_events().removeHandler('OnClientTextChanged', handler); },

	_raiseEvent: function(eventName, eventArgs) {
		var handler = this.get_events().getHandler(eventName);
		if (handler) {
			if (!eventArgs) { eventArgs = Sys.EventArgs.Empty; }
			handler(this, eventArgs);
		}
	},

	get_Save: function() { return this._Save; },
	set_Save: function(value) { this._Save = value; },

	get_Cancel: function() { return this._Cancel; },
	set_Cancel: function(value) { this._Cancel = value; },

	get_ContextData1: function() { return this._ContextData1; },
	set_ContextData1: function(value) { this._ContextData1 = value; },

	get_ContextData2: function() { return this._ContextData2; },
	set_ContextData2: function(value) { this._ContextData2 = value; },

	get_ContextData3: function() { return this._ContextData3; },
	set_ContextData3: function(value) { this._ContextData3 = value; },

	get_IsMultiLine: function() { return this._IsMultiLine; },
	set_IsMultiLine: function(value) { this._IsMultiLine = value; },

	get_TooltipBehaviorId: function() { return this._TooltipBehaviorId; },
	set_TooltipBehaviorId: function(value) { this._TooltipBehaviorId = value; }
}
OpenPhotoAlbum.Controls.InplaceEditLabel.registerClass('OpenPhotoAlbum.Controls.InplaceEditLabel', AjaxControlToolkit.ControlBase);
